package com.gitee.feizns.explore.data_structure.string;

import java.util.HashSet;
import java.util.Set;

/**
 * 345. 反转字符串中的元音字母
 * @author feizns
 * @since 2020/1/16
 */
public class ReverseVowels {

    public static void main(String[] args) {
        System.out.println(reverseVowels("hello"));
    }

    public static String reverseVowels(String s) {
        char[] chars = s.toCharArray();
        if ( chars.length > 0 ) {
            int l = 0;
            int r = chars.length - 1;
            while ( l < r ) {
                while ( l < r && !VOWEL.contains(chars[l]) ) { l++; }
                while ( l < r && !VOWEL.contains(chars[r]) ) { r--; }
                if ( l < r ) {
                    swap(chars, l, r);
                    l++;
                    r--;
                }
            }
        }
        return new String(chars);
    }

    private static void swap(char[] c, int i, int j) {
        char tmp = c[i];
        c[i] = c[j];
        c[j] = tmp;
    }

    private static Set<Character> VOWEL = new HashSet<>();

    static {
        VOWEL.add('a');
        VOWEL.add('e');
        VOWEL.add('i');
        VOWEL.add('o');
        VOWEL.add('u');
    }

}
